var express=require('express')
var routers=express. Router()
var mongo=require('./mongo')
var loginto=function(req,res){
    if (req.session.userinfo==undefined){
        res.send('<script>alert(\'您还没有登录，请登录\');history.back()</script>')
        res.redirect('/login');
    }
}
        //登录路由
        routers.get('/login',function(req,res){
                res.render('login.html')
            })
        routers.post('/login',function(req,res){
            let name=req.body.name
            let password=req.body.password
            if(name=='admin'&&password=='123456'){
                req.session.userinfo = name
                res.json({
                    sessce:'登录成功',
                    status:'200',
                    massge:'0'
                })
            }else if(name =='admin' || password!='123456'){
                res.json({
                    err:'密码错误',
                    status:'200',
                    massge:'1'
                })
            }else if(name !='admin' || password =='123456'){
                res.json({
                    err:'用户名错误',
                    status:'200',
                    massge:'1'
                })
            }
        })
            //退出登录
            routers.get('/tuichu',function(req,res){
                req.session.userinfo=undefined
                res.redirect('/login');
            })

        //功能也路由
        routers.get('/student',function(req,res){
            loginto(req,res)
            mongo.find()
                .then(function(data){
                    console.log(data.length)
                        res.render('page.html', {
                            students: data,
                            userinfo: req.session.userinfo
                        })
                })
                .catch(()=>res.status(500).send("Server err"))


        })
        //增加学生get路由
        routers.get('/student/new',function(req,res){
            loginto(req,res)
            res.render('add.html')
        })
        //增加学生post路由
        routers.post('/student/new',function(req,res){
            /*模型实例化*/
            const mongod = new mongo(req.body);
            /*实例.save((err,docs)=>{回调函数}) 来将数据保存到数据库*/
            mongod.save((err, docs)=>{
                if(err){
                    console.log('添加数据出错');
                    return;
                }
                res.redirect('/student');
                console.log(docs);
            });
        })
        //修改学生get路由
        routers.get('/student/alter',function(req,res){
            loginto(req,res)
            mongo.findById(req.query.id,function(err,student){
            if(err){
                return res.status(500).send("Server err")
            }
                res.render('alter.html',{
                    student:student
                })
            })

        })
        //修改学生post路由
        routers.post('/student/alter',function(req,res){
            console.log(req.body.id)
            mongo.updateOne(
                {'_id':req.body.id},
                {
                name:req.body.name,
                gender:req.body.gender,
                age:req.body.age,
                height:req.body.height,
                hobbies:req.body.hobbies
             },function(err){
                if(err){
                    return res.status(500).send("Server err")
                }
                res.redirect('/student');
            })
        })
        //删除学生post路由
        routers.get('/student/delete',function(req,res){
            loginto(req,res)
            console.log(req.body)
            mongo.deleteOne({'_id':req.query.id},function(err){
                if(err){
                   console.log('删除失败')
                }
                res.redirect('/student');
            })

        })



module.exports=routers
